package com.example.cheeseerpadmin.sys.common;

import cn.hutool.core.util.IdUtil;
import org.aspectj.util.FileUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Properties;
public class WebFileUtils {
    //文件上传的保存路径
    public static String UPLOAD_PATH = "E:/IdeaProject/upload/";

    static {
        //读取配置文件的存储地址
        InputStream resourceAsStream = WebFileUtils.class.getClassLoader().getResourceAsStream("file.properties");
        Properties properties = new Properties();
        try {
            //然后加载这个输入流
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //从这被加载的输入流当中获取配置
        String filepath = properties.getProperty("filepath");
        if (filepath != null) {
            UPLOAD_PATH = filepath;
        }
    }

    /**
     * 根据老名字拼接一个新的名字出来
     *
     * @param oldName
     * @return
     */
    public static String createNewFileName(String oldName) {
        String substring = oldName.substring(oldName.lastIndexOf("."), oldName.length());
        return IdUtil.simpleUUID().toUpperCase(Locale.ROOT) + substring;
    }

    /**
     * 图片下载
     *
     * @param path
     * @return
     */
    public static ResponseEntity<Object> createResponseEntity(String path) {
        //根据路径创建一个文件对象
        File file = new File(UPLOAD_PATH, path);
        //然后根据这个文件读取，得到一个byte[]
        byte[] bytes = null;
        try {
            bytes = FileUtil.readAsByteArray(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //创建封装响应头信息的对象
        HttpHeaders httpHeaders = new HttpHeaders();
        //封装响应内容类型（APPLICATION_OCTET_STREAM 响应的内容不限定）
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //设置下载的文件的名称
        httpHeaders.setContentDispositionFormData("attachment", "123.jpg");
        //创建ResponseEntity对象
        ResponseEntity<Object> objectResponseEntity = new ResponseEntity<Object>(bytes, httpHeaders, HttpStatus.CREATED);
        return objectResponseEntity;
    }

    /**
     * 删除图片
     *
     * @param goodsimg
     */
    public static void removeFileByPath(String goodsimg) {
        if (!goodsimg.equals(Constant.IMAGES_DEFAULTGOODSIMG_PNG)) {
            File file = new File(UPLOAD_PATH, goodsimg);
            if (file.exists()) {
                file.delete();
            }
        }
    }

    /**
     * 批量删除图片
     *
     * @param goodsimgs
     */
    public static void removeFileByPaths(String[] goodsimgs) {
        for (String goodsimg : goodsimgs) {
            if (!goodsimg.equals(Constant.IMAGES_DEFAULTGOODSIMG_PNG)) {
                File file = new File(UPLOAD_PATH, goodsimg);
                if (file.exists()) {
                    file.delete();
                }
            }
        }
    }

    public static String renameFile(String goodsimg) {
        File file = new File(UPLOAD_PATH, goodsimg);
        String replace = goodsimg.replace("_temp", " ");
        if (file.exists()){
            file.renameTo(new File(UPLOAD_PATH, replace));
        }
        return replace;
    }
}
